//
//  TopScrollView.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/1.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "TopScrollView.h"

#define btnWidth kWidth/5

@implementation TopScrollView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _currentIndex = 0;
        self.backgroundColor = [UIColor colorWithRed:230.0/255 green:230.0/255 blue:230.0/255 alpha:1];
        [self _addSubViews];
    }
    return self;
}

- (void)_addSubViews {
    //说是说btn，其实不是.只是一些label，通过touchBegan获取到点击的位置，然后控制相应的label改变显示的大小和颜色.
    //同时也根据collectionViewCell的切换，随之切换
    NSArray* labelNameArray = @[@"头条",@"NBA",@"娱乐",@"时尚",@"汽车",@"房产",@"段子"];
    for (int i = 0; i < labelNameArray.count; i++) {
        UILabel* label =[[UILabel alloc] initWithFrame:CGRectMake(btnWidth*i, 0, btnWidth, 30)];
        label.text = labelNameArray[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:12];
        label.tag = i+100;
        [self addSubview:label];
        
        if (i == 0) {           //初始时先设置第一个导航按钮
            label.textColor = kNetEaseRedColor;
            label.font = [UIFont systemFontOfSize:15];
        }
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* touch = [touches anyObject];
    
    CGPoint point = [touch locationInView:self];
    
    NSInteger index = point.x/kWidth*5;
    if (_currentIndex != index) {            //如果变化了序号
        [self setCurrentIndex:index];
    }//否则啥也不干
}

//复写set方法，设置后切换
- (void)setCurrentIndex:(NSInteger)currentIndex {
    if (_currentIndex != currentIndex) {
        UILabel* oldLabel = (UILabel*)[self viewWithTag:_currentIndex+100];
        UILabel* newLabel = (UILabel*)[self viewWithTag:currentIndex+100];
        
        [UIView animateWithDuration:0.5 animations:^{
            oldLabel.textColor = [UIColor blackColor];
            oldLabel.font = [UIFont systemFontOfSize:12];
        } completion:^(BOOL finished) {
            if (finished) {
                [UIView animateWithDuration:0.5 animations:^{
                    newLabel.textColor = kNetEaseRedColor;
                    newLabel.font = [UIFont systemFontOfSize:15];
                }];
            }
        }];
        
        [UIView animateWithDuration:0.5 animations:^{
            if (currentIndex == 0 || currentIndex == 1) {       //让2在中间
                self.contentOffset = CGPointMake(0, 0);
            } else if (currentIndex == 2 || currentIndex == 3) {        //是谁谁就在中间
                self.contentOffset = CGPointMake((currentIndex-2)*btnWidth, 0);
            } else {                                //让4在中间
                self.contentOffset = CGPointMake(2*btnWidth, 0);
            }
        }];
        
        //发送通知，告诉collectionView切换
        NSDictionary* userInfo = @{@"index":@(currentIndex)};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TopScrollViewIndexChangedNotification" object:nil userInfo:userInfo];
        
        _currentIndex = currentIndex;
    }
}


@end
